package db

import (
	"errors"
	"log"
	"server_cluster/battle/sharemem"
	"strconv"
	"strings"
)

type Config_monster struct {
	Id               int32  `gorm:"primary_key;column:id;autoIncrement:false" csv:"Id"`
	MonsterCodexType int32  `gorm:"column:monster_type" csv:"Pictorialtype"` // 怪物图鉴类型
	Skills           string `gorm:"column:skills" csv:"Skills"`
	Ex_skill         int32  `gorm:"column:ex_skill" csv:"Ex_Skill,omitempty"`
	Passive          string `gorm:"column:passive" csv:"Passive,omitempty"`
	Tough_time       int32  `gorm:"column:tough_time" csv:"ToughTime,omitempty"`
	Tough_buff       string `gorm:"column:tough_buff" csv:"ToughBuff,omitempty"`
	Type             int8   `gorm:"column:type" csv:"Type"`
	Weak_point       string `gorm:"column:weak_point" csv:"Weak_Point"`
	Attr             int32  `gorm:"column:attr" csv:"Attr"`
}

func (tmp *Config_monster) TableName() string {
	return "config_monster"
}

func (tmp *Config_monster) getAll() ([]*Config_monster, error) {
	var data []*Config_monster
	result := Conn.Find(&data)

	return data, result.Error
}

func (tmp *Config_monster) LoadData() error {
	data, err := tmp.getAll()
	if err != nil {
		return err
	}
	var myshm = &(sharemem.Myshm.Cfg.Mst)
	for key, value := range data {
		if key >= sharemem.MONSTER_MAX {
			return errors.New("config_monster format error 8")
		}
		myshm[key].Id = value.Id
		if len(value.Skills) > 0 {
			a_list := strings.Split(value.Skills, ";")
			if len(a_list) > sharemem.UNIT_ORDERS_MAX {
				return errors.New("config_monster format error 10")
			}
			for k, v := range a_list {
				r_list := strings.Split(v, "|")
				if len(r_list) != 2 {
					return errors.New("config_monster format error 3")
				}
				arg1, err := strconv.Atoi(r_list[0])
				if err != nil {
					return errors.New("config_monster format error 4")
				}
				arg2, err := strconv.Atoi(r_list[1])
				if err != nil {
					return errors.New("config_monster format error 5")
				}
				myshm[key].Order[k].Id = int32(arg1)
				myshm[key].Order[k].Time = int32(arg2)
			}
		}
		myshm[key].Ex_skill = value.Ex_skill
		if len(value.Passive) > 0 {
			list := strings.Split(value.Passive, ";")
			if len(list) > sharemem.UNIT_PASSIVE_MAX {
				return errors.New("config_monster format error 8")
			}
			for k, v := range list {
				arg, err := strconv.Atoi(v)
				if err != nil {
					return errors.New("config_monster format error 9")
				}
				myshm[key].Passive[k] = int32(arg)
			}
		}
		myshm[key].Tough_time = value.Tough_time
		if len(value.Tough_buff) > 0 {
			list := strings.Split(value.Tough_buff, ";")
			if len(list) > sharemem.UNIT_TOUGHBUFF_MAX {
				return errors.New("config_monster format error 8")
			}
			for k, v := range list {
				arg, err := strconv.Atoi(v)
				if err != nil {
					return errors.New("config_monster format error 9")
				}
				myshm[key].Tough_buff[k] = int32(arg)
			}
		}
		myshm[key].Type = value.Type
		if len(value.Weak_point) > 0 {
			list := strings.Split(value.Weak_point, ";")
			if len(list) > sharemem.UNIT_WEAK_MAX {
				return errors.New("config_monster format error 7")
			}
			for k, v := range list {
				arg, err := strconv.Atoi(v)
				if err != nil {
					return errors.New("config_monster format error 1")
				}
				myshm[key].Weak_point[k] = int8(arg)
			}
		}
		myshm[key].Attr = value.Attr

		//
	}

	log.Println("load config_monster completed.")

	return nil
}
